package com.yc.navy.goods.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
public class SendEmailCode {
	@Value("2972439824@qq.com")
	private String email;
	
	@Autowired
	private JavaMailSender send;
	
	public boolean sendEmail(String ClientEmail,String code , String nickName) {
		if(StringUtil.checkNull(ClientEmail,code,nickName)) {
			return false;
		}
		try {
			MimeMessage mimeMessage = send.createMimeMessage();
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
			
			mimeMessageHelper.setTo(ClientEmail);
			mimeMessageHelper.setFrom(email);
			
			mimeMessageHelper.setSubject("Fizz");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE");
			String str = "<!DOCTYPE html><html><head><meta charset='UTF-8'></head><body><p style='font-size: 20px;font-weight:bold;'>尊敬的："+nickName+"，您好！</p>"
					+ "<p style='text-indent:2em; font-size: 20px;'>欢迎来到Fizz，您本次的注册码是 "
					+ "<span style='font-size:30px;font-weight:bold;color:red'>"+code+"</span>，3分钟之内有效，请尽快使用！</p>"
					+ "<p style='text-align:right; padding-right: 20px;'>"
					+ "<a href='http://www.hyycinfo.com' style='font-size:18px'>衡阳市强盛集团有限公司</a></p>"
					+ "<span style='font-size:18px; float:right; margin-right: 20px;'>"+sdf.format(new Date())+"</span></body></html>";
			mimeMessageHelper.setText(str,true);
			send.send(mimeMessage);
			return true;
		} catch (MailException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return false;
	}

}
